package com.tiyende.credit.loop.utils.helper

import com.tiyende.credit.loop.BuildConfig
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject

/**
 * ConfigsHelper
 * @Author wwdeng
 * @CreateTime: 2024/11/28 12:04
 * @Annotation：自定义数据 帮助类
 */
object TiyendeConfigsHelper {
    //缓存弹出公告信息的次数，此值只会在：审核中、放款中、待还款改变
    var showNoticeNum = 0

    /**
     * 自定义数据来源
     *     1：客服配置
     *
     * "customData": {
     *         "workDate": "Monday to Sunday",
     *         "workTime": "9:00am to 6:30pm"
     *         "noticeInfo": {
     *             "num": 3,
     *             "isShow": "YES",
     *             "noticeTitle": "Notice",
     *             "noticeText": "Please do not transfer repayments to any private account to avoid fraud and loss!"
     *         },
     *         "release":{
     *              "isMustUpdate":true,
     *              "versionCode":"1",
     *              "versionName":"1.0.0",
     *              "versionRemark":""//更新描述 需用"$"分割
     *         }
     *     },
     *
     */

    /**
     * 获取客服配置
     */
    fun queryTiyendeConfigData(): JSONObject? {
        val dataString = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun queryTiyendeCustomData(): JSONObject?{
        val customize = queryTiyendeConfigData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 获取启动权限
     */
    fun queryTiyendePermissionsLaunch(): ArrayList<String>{
        val permissionLaunchList = arrayListOf<String>()
        val customize = queryTiyendeConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                val array = permissions.optJSONArray("permission")
                if (array != null) {
                    for (i in 0..<array.length()){
                        permissionLaunchList.add(array.optString(i))
                    }
                }
            }
        }
        return permissionLaunchList
    }

    /**
     * 获取首页权限
     */
    fun queryTiyendePermissionsHome(): ArrayList<String>{
        val permissionHomeList = arrayListOf<String>()
        val customize = queryTiyendeConfigData()
        if(customize != null) {
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null) {
                val array = permissions.optJSONArray("home")
                if (array != null) {
                    for (i in 0..<array.length()){
                        permissionHomeList.add(array.optString(i))
                    }
                }
            }
        }
        return permissionHomeList
    }

    /**
     * 获取所有权限列表 = 启动权限 + 首页权限
     */
    fun queryTiyendePermissionsList(): ArrayList<String>{
        val permissionList = arrayListOf<String>()
        //启动权限
        val permissionLaunch = queryTiyendePermissionsLaunch()
        if (permissionLaunch.size > 0){
            permissionList.addAll(permissionLaunch)
        }

        //首页权限
        val permissionHome = queryTiyendePermissionsHome()
        if (permissionHome.size > 0){
            for (i in 0..<permissionHome.size){
                val str = permissionHome[i]
                if (permissionList.contains(str)){
                    continue
                }
                permissionList.add(str)
            }
        }
        return permissionList
    }

    /**
     * 审核中、放款中、待还款是否显示公告信息
     */
    fun isTiyendeShowConfigNotice(): Boolean{
        val customData = queryTiyendeCustomData() ?: return false
        if (customData.has("noticeInfo")){
            val noticeInfo = customData.optJSONObject("noticeInfo") ?: return false
            return noticeInfo.optString("isShow","NO") == "YES"
        }
        return false
    }

    /**
     * 获取 审核中、放款中 显示的公告信息
     */
    fun queryTiyendeConfigNoticeInfo(): JSONObject?{
        val customData = queryTiyendeCustomData() ?: return null

        var noticeData: JSONObject? = null
        if (customData.has("noticeInfo")){
            //默认为英语
            noticeData = customData.optJSONObject("noticeInfo")
        }
        return noticeData
    }

    /**
     * SMS Content 关键字过滤
     */
    fun queryTiyendeSmsContentKeys(): ArrayList<String>?{
        val customData = queryTiyendeCustomData() ?: return null
        if (customData.has("smsContentKeys")){
            val smsContentKeys = customData.optJSONArray("smsContentKeys")
            if (smsContentKeys != null) {
                val keysList = arrayListOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysList.add(smsContentKeys.optString(i))
                }
                return keysList
            }
        }
        return null
    }


    /**
     * SMS Mobile 关键字过滤
     */
    fun queryTiyendeSmsMobileKeys(): ArrayList<String>?{
        val customData = queryTiyendeCustomData() ?: return null
        if (customData.has("smsMobileKeys")){
            val smsContentKeys = customData.optJSONArray("smsMobileKeys")
            if (smsContentKeys != null) {
                val keysList = arrayListOf<String>()
                for (i in 0..<smsContentKeys.length()){
                    keysList.add(smsContentKeys.optString(i))
                }
                return keysList
            }
        }
        return null
    }

    /**
     * 获取版本更新信息
     */
    fun queryTiyendeVersionObject(): JSONObject?{
        val customData = queryTiyendeCustomData() ?: return null
        if (!customData.has("release")){
            return null
        }
        return customData.optJSONObject("release")
    }

    /**
     * 获取是否有强制更新（启动页使用）
     *  "isMustUpdate":true,
     *  "versionCode":"1",
     *  "versionName":"1.0.0",
     *  "versionRemark":"1xxx$2xxx$3xxx"
     */
    fun isTiyendeUpdateMust(): Boolean {
        val versionObj = queryTiyendeVersionObject() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        val isMustUpdate = versionObj.optBoolean("isMustUpdate",false)
        return BuildConfig.VERSION_CODE < versionCode && isMustUpdate
    }

    /**
     * 获取是否有更新（首页使用）
     */
    fun isTiyendeUpdate(): Boolean{
        val versionObj = queryTiyendeVersionObject() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        return BuildConfig.VERSION_CODE < versionCode
    }
}